<template>
  <!-- 日期栏 -->
  <view class="dept-week">
    <view class="dept-week-box" v-for="(item, index) of weeks" :key="item" @click="chooeseDate(index)">
      <view class="dept-week-box-day">
        {{item.day}}
      </view>
      <view :class="[item.active ? 'dept-week-box-date-active' : '', 'dept-week-box-date']">
        {{item.date}}
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: "WeekdayBar",
  props: {
    offsetDays: {  // 日期偏移量
      type: Number,
      default: 0
    },
    indexDay: {  // 默认选中日期索引
      type: Number,
      default: 0
    }
  },
	data() {
		return {
      weeks: []
		};
	},
  mounted: function() {
    this.initDate();
  },
	methods: {
    initDate: function() {  // 初试化日期栏内容
      this.weeks.length = 0;
      /* this.weeks.push({
        date: this.Base.getDate(this.offsetDays).split("-")[2],
        day: this.getWeek(this.Base.getDate(this.offsetDays)),
        active: true
      }); */
      for(var i = 0; i < 7; i++) {
        this.weeks.push({
            date: this.Base.getDate(this.offsetDays + i).split("-")[2],
            day: this.getWeek(this.Base.getDate(this.offsetDays + i)),
            active: i == this.indexDay ? true : false
        });
      }
    },
		chooeseDate: function(index) {  // 选择日期
		  for(var i = 0, len = this.weeks.length; i < len; i++) {
		    this.weeks[i]["active"] = index == i ? true : false;
		  }
      this.$emit('choose-date', index);
		},
    getWeek: function(date) {
      var d = (new Date(date)).getDay();
      switch(d) {
        case 0 : return "日"; break;
        case 1 : return "一"; break;
        case 2 : return "二"; break;
        case 3 : return "三"; break;
        case 4 : return "四"; break;
        case 5 : return "五"; break;                                                             
        case 6 : return "六"; break;
        default : return null; break;
      }
    }
	}
};  
</script>

<style lang="scss" scoped>
  .dept-week {
    font-size: 14px;
    position: relative;
    width: 100%;
    height: auto;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    background-color: #FFFFFF;
  }
  .dept-week-box {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }
  
  .dept-week-box-day {
    font-size: $uni-font-size-base;
  }
  
  .dept-week-box-date {
    padding: 6upx;
    border-radius: 50%;
    border: 1px solid #999;
    margin: 10upx auto;
    width: 33upx;
    height: 33upx;
    line-height: 33upx;
    text-align: center;
    font-size: $uni-font-size-base;
  }
  .dept-week-box-date-active {
    background-color: #009E7E;
  }
  .dept-week:after {
    content: " ";
    position: absolute;
    width: 100%;
    bottom: 0;
    height: 1px;
    background-color: #D8D8D8;
  }
</style>
